package com.share.four.netty.heartBeat.sigar;

import java.net.InetAddress;
import java.util.Map;
import java.util.Properties;

import org.hyperic.sigar.CpuInfo;
import org.hyperic.sigar.CpuPerc;
import org.hyperic.sigar.Mem;
import org.hyperic.sigar.OperatingSystem;
import org.hyperic.sigar.Sigar;
import org.hyperic.sigar.Swap;

public class SigarTest {
	
	public static void main(String[] args) {
		try {
			//system信息，从jvm中获取
			property();
			System.out.println("=========================");
			
			//cpu信息，从sigar中获取
			//cpu();
			System.out.println("=========================");
			
			//内存信息，从sigar中获取
			//memory();
			System.out.println("=========================");
			
			//操作系统信息，从sigar中获取
			os();
			System.out.println("=========================");
			
			
			
		} catch (Exception e) {
			System.out.println(e);
		}
	}

	private static void os() throws Exception {
		OperatingSystem os = OperatingSystem.getInstance();
		System.out.println("操作系统：        " + os.getArch());
		System.out.println("操作系统CpuEndian()：        " + os.getCpuEndian());
		System.out.println("操作系统DataModel：        " + os.getDataModel());
		System.out.println("操作系统描述：        " + os.getDescription());
		System.out.println("操作系统版本号：        " + os.getVersion());
		
	}

	private static void memory() throws Exception {
		Sigar sigar = new Sigar();
		Mem mem = sigar.getMem();
		System.out.println("内存总量：		" + mem.getTotal()/1024 + "K av"); 
		System.out.println("当前内存使用量：		" + mem.getUsed()/1024 + "K used"); 
		System.out.println("当前内存剩余量：		" + mem.getFree()/1024 + "K free"); 
		System.out.println("--------------------------------------------");
		
		Swap swap = sigar.getSwap();
		System.out.println("交换区总量：		" + swap.getTotal()/1024 + "K av"); 
		System.out.println("当前交换区使用量：		" + swap.getUsed()/1024 + "K used"); 
		System.out.println("当前交换区剩余量：		" + swap.getFree()/1024 + "K free"); 
	}

	private static void cpu() throws Exception {
		Sigar sigar = new Sigar();
		System.out.println("cpu总参数情况：	" + sigar.getCpu());
		System.out.println("cpu总百分比情况：	" + sigar.getCpuPerc());
		System.out.println("--------------------------------------------");
		
		CpuInfo[] infos = sigar.getCpuInfoList();
		CpuPerc[] percs = sigar.getCpuPercList();
		
		for (int i = 0; i < infos.length; i++) {
			CpuInfo cpuInfo = infos[i];
			CpuPerc cpuPerc = percs[i];
			System.out.println("第" + (i+1) + "块cpu信息");
			System.out.println("CPU的总量MHz：	" + cpuInfo.getMhz());
			System.out.println("CPU生产商：	" + cpuInfo.getVendor());
			System.out.println("CPU类别：		"+ cpuInfo.getModel());
			System.out.println("CPU缓存数量：	" + cpuInfo.getCacheSize());
			System.out.println("");
			System.out.println("CPU用户使用率：	" + CpuPerc.format(cpuPerc.getUser()));
			System.out.println("CPU系统使用率：	" + CpuPerc.format(cpuPerc.getSys()));
			System.out.println("CPU当前等待率：	" + CpuPerc.format(cpuPerc.getWait()));
			System.out.println("CPU当前错误率：	" + CpuPerc.format(cpuPerc.getNice()));
			System.out.println("CPU当前空闲率：	" + CpuPerc.format(cpuPerc.getIdle()));
			System.out.println("CPU总的使用率：	" + CpuPerc.format(cpuPerc.getCombined()));
		}
		
	}

	private static void property() throws Exception {
		InetAddress localHost = InetAddress.getLocalHost();
		System.out.println("本地ip地址:	" + localHost.getHostAddress());
		System.out.println("本地主机名:	" + localHost.getHostName());
		System.out.println("--------------------------------------------");
		
		Runtime r = Runtime.getRuntime();
		System.out.println("JVM可以使用的总内存：	" + r.totalMemory());
		System.out.println("JVM可以使用的剩余内存：	" + r.freeMemory());
		System.out.println("JVM可以使用的处理器个数：	" + r.availableProcessors());
		System.out.println("--------------------------------------------");
		
		Map<String, String> map = System.getenv();
		System.out.println("用户名：	" + map.get("USERNAME"));
		System.out.println("计算机名：	" + map.get("COMPUTERNAME"));
		System.out.println("计算机域名：	" + map.get("USERDOMAIN"));
		System.out.println("--------------------------------------------");
		
		Properties props = System.getProperties();
		System.out.println("java的安装路径：	" + props.getProperty("java.home"));
		System.out.println("java的运行环境版本：	" + props.getProperty("java.version"));
		System.out.println("操作系统名称：	" + props.getProperty("os.name"));
		System.out.println("操作系统版本：	" + props.getProperty("os.version"));
		System.out.println("用户账户名称：		" + props.getProperty("user.name"));
		System.out.println("用户的主目录：		" + props.getProperty("user.home"));
		System.out.println("用户的当前工作目录：		" + props.getProperty("user.dir"));
		System.out.println("--------------------------------------------");
		
		
	}

}
